package com.sx.wechat.command.api;

import com.sx.core.model.Order;
import com.sx.core.mybatis.SessionTemplate;
import com.sx.core.page.Page;
import com.sx.core.vo.JsonReaderResponse;
import com.sx.wechat.command.cmd.AbstractCommand;
import com.sx.wechat.security.AuthenticationHolder;
import com.sx.wechat.security.CoreWebUtils;
import com.sx.wechat.security.Principal;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 挂失解挂
 * Created by lihao on 2018/3/21.
 */
@Component("list_record")
public class List_record extends AbstractCommand {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    SessionTemplate sessionTemplate;

    @Override
    public Object executeApi(HttpServletRequest request, HttpServletResponse response) {
        Principal principal= AuthenticationHolder.getCurrent();
        int page=obtainPage(request);
        int limit=obtainLimit(request);
        Map<String,Object> prams= CoreWebUtils.requestMap(request);
        prams.put("types",new String[]{"3"});

        if(StringUtils.equals(principal.getType(),"2")){
            prams.put("teacherid",principal.getId());
        }else if(StringUtils.equals(principal.getType(),"1")){
            List<Map<String,Object>> exist=jdbcTemplate.queryForList("select * from t_order_rec_stu WHERE stuid=?",principal.getId());
            List<String> ids=new ArrayList<>();
            for(Map<String,Object> map:exist){
                ids.add(ObjectUtils.toString(map.get("orderid")));
            }
            prams.put("ids",ids);
        }
        Page<Order> listPage=sessionTemplate.listPage(Order.class,"list_order",page,limit,prams);
        return new JsonReaderResponse(listPage.getContent(),listPage.getTotalElements());
    }

}
